package com.example.ssm.Conn;

import com.example.ssm.Dao.AccountMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @create 2022-03-2022/3/6-15:07
 */
public class SqlConn {
    private InputStream is =null;
    private SqlSessionFactoryBuilder builder=null;
    private SqlSessionFactory factory=null;
    private SqlSession sqlSession=null;
    private AccountMapper accountMapper=null;

    public AccountMapper   conn() throws IOException {
        is = Resources.getResourceAsStream("SqlMapperConfig.xml");
        // 2. 创建 SqlSessionFactory 工厂
        builder = new SqlSessionFactoryBuilder();
        factory = builder.build(is);
        // 3. 获取 SqlSession 对象
        sqlSession = factory.openSession();
        // 4. 使用 SqlSession 创建 Mapper 的代理对象
        accountMapper=sqlSession.getMapper(AccountMapper.class);
        return  accountMapper;
    }

    public void close() throws IOException {
        sqlSession.commit();
        sqlSession.close();
        is.close();
    }
}
